package com.sr.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sr.entity.Author;
import com.sr.util.BaseDao;

public class AuthorDaoImp {
	//登录验证
	public boolean getByName(String username,String password){
		try {
			Connection conn = BaseDao.getConnection();
			PreparedStatement ps = conn.prepareStatement("select * from author where username=? and password=?");
		    ps.setString(1, username);
		    ps.setString(2, password);
		    ResultSet rs=ps.executeQuery();
		    Author author=null;
		    while(rs.next()){
		    	author=new Author();
		    	author.setId(rs.getInt(1));
		    	author.setUserName(rs.getString(2));
		    	author.setPassword(rs.getString(3));
		    	if(username!=null && password!=null){
					 return true;
				 }else{
					 return false;
				 }
		    }
		    
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return false;
		
	}
	
	//注册验证(添加作者用户)
	public boolean addAuthor(Author author){
		Connection conn=BaseDao.getConnection();
		try {
			PreparedStatement ps=conn.prepareStatement("insert into author(username,password,idcard,authorpenname) values(?,?,?,?)");
			ps.setString(1, author.getUserName());
			ps.setString(2, author.getPassword());
			ps.setString(3, author.getIdCard());
			ps.setString(4, author.getAuthorPenName());
			int rs=ps.executeUpdate();
			if(rs>0){
				return true;
			}else{
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		
	}
	//判断是否存在这个用户
	public boolean isExist(String username){
		return findUserName(username)==null?false:true;
		
	}
	//根据username查找用户
	public Author findUserName(String username){
		try {
			Connection conn = BaseDao.getConnection();
			PreparedStatement ps = conn.prepareStatement("select * from author where username=?");
		    ps.setString(1, username);
		    ResultSet rs=ps.executeQuery();
		    Author author=null;
		    while(rs.next()){
		    	author=new Author();
		    	author.setId(rs.getInt(1));
		    	author.setUserName(rs.getString(2));
		    	author.setPassword(rs.getString(3));
		    	author.setIdCard(rs.getString(4));
		    	author.setAuthorPenName(rs.getString(5));
		    	author.setNumberOfWorks(rs.getInt(6));
		    	author.setNumberOfFans(rs.getInt(7));
		    	author.setListimg(rs.getString(8));
		    	System.out.println(author);
		    }
		    return author;
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return null;
		
	}
	//修改用户信息
	public boolean update(Author author){
		Connection conn=BaseDao.getConnection();
		try {
			PreparedStatement ps=conn.prepareStatement("update author set username=?,idcard=?,authorpenname=?  where id=? ");
			ps.setString(1, author.getUserName());
			ps.setString(3, author.getAuthorPenName());
			ps.setString(2, author.getIdCard());
			ps.setInt(4, author.getId());
			int rs=ps.executeUpdate();
			System.out.println(rs);
		    if(rs>0){
		    	System.out.println("修改成功");
					return true;
			}else{
				System.out.println("修改失败");
					return false;
			}
			}catch(Exception e){
					e.printStackTrace();
					return false;
			}
	
	}
	}
